package com.zhao.controller;

import com.sun.org.apache.xpath.internal.operations.Bool;
import com.zhao.Others.FileOperationObject;
import com.zhao.dao.EbickDao;
import com.zhao.pojo.Ebick;
import com.zhao.pojo.Superviser;
import com.zhao.pojo.User;
import com.zhao.service.DrivingLicenseService;
import com.zhao.utils.QRCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.File;

@Controller
public class QRController {


    @Autowired
    private EbickDao ebickDao;

    @Autowired
    private DrivingLicenseService drivingLicenseService;

    @ResponseBody
    @RequestMapping("encode-qrcode.do")
    public String encodeQRcode(String content){

        String destPath = ClassUtils.getDefaultClassLoader().getResource("static").getPath()+"/QRCode/" + System.currentTimeMillis() + ".jpg";
        //生成二维码
        try {
            QRCodeUtil.encode(content, null, destPath, true);
            String visitPath = destPath.substring(destPath.indexOf("QRCode",0));
            System.out.println(visitPath);
            return visitPath;
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }

    }

    @ResponseBody
    @RequestMapping("decode-qrcode.do")
    public String decodeQRcode(MultipartFile qrcode,HttpSession session){
        System.out.println("start decode");
        User user = (User)session.getAttribute("user");
        System.out.println("getOriginalFilename："+qrcode.getOriginalFilename());
        String visitPath = FileOperationObject.savePicture(qrcode,"QRCode","001",user.getUserID());
        System.out.println("visitPath: "+visitPath);
        String res ="error";
        visitPath = "/D:/IdeaProjects/NewEbickManagement/target/classes/static/"+visitPath;

        try {
            //灰度化
            File file = new File(visitPath );
            BufferedImage image = ImageIO.read(file);

            int width = image.getWidth();
            int height = image.getHeight();

            BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
            for ( int i= 0 ; i < width ; i++){
                for ( int j = 0 ; j < height; j++){
                    int rgb = image.getRGB(i, j);
                    grayImage.setRGB(i, j, rgb);
                }
            }

            File newFile = new File(ClassUtils.getDefaultClassLoader().getResource("static").getPath()+"/QRCode/"+ "/method1.jpg" );
            ImageIO.write(grayImage, "jpg" , newFile);

            visitPath=ClassUtils.getDefaultClassLoader().getResource("static").getPath()+"/QRCode/"+ "/method1.jpg";
            res = QRCodeUtil.decode(visitPath);
            System.out.println("decode-res: "+res);
            return res;
        }catch (Exception e){
            System.out.println("decode error");
            e.printStackTrace();
            return "error";
        }
        finally {

            File file = new File(visitPath);
            if(file.exists()){
                Boolean flag = file.delete();
                if (flag){
                    System.out.println("成功删除图片"+file.getName());
                }else {
                    System.out.println("删除失败");
                }
            }
            file = new File(ClassUtils.getDefaultClassLoader().getResource("static").getPath()+"/QRCode/"+ "/method1.jpg");
            if(file.exists()){
                Boolean flag = file.delete();
                if (flag){
                    System.out.println("成功删除图片"+file.getName());
                }else {
                    System.out.println("删除失败");
                }
            }
        }
    }

    @RequestMapping("superviser-scan-getEbick")
    public String superviserScanGetEbick(String ebickID,String universityID,String toWhere,HttpSession session){

        System.out.println(toWhere);
        Ebick ebick = ebickDao.findByID(ebickID,universityID);
        if(ebick==null){
            return "404";
        }
        session.setAttribute("scanEbick",ebick);

        Superviser superviser = (Superviser)session.getAttribute("superviser");
        if(superviser!=null){
            String res = drivingLicenseService.superviserFindDrivingLicense(ebickID,session);
            System.out.println(res);
        }

        return "redirect:/"+toWhere;
    }

    @RequestMapping("superviser-toScanQR.do")
    public String superviserToScanQR(HttpSession session){


        return "";
    }

    @ResponseBody
    @RequestMapping("user-shibieQR.do")
    public String supervisershibieQR(HttpSession session, MultipartFile file){

        System.out.println(file.getOriginalFilename());
        String visitPath = FileOperationObject.savePicture(file,"QRCode","001","160120010117");
        System.out.println(visitPath);
        String res ="error";
        visitPath = "/D:/IdeaProjects/NewEbickManagement/target/classes/static/"+visitPath;
        try{
            res = QRCodeUtil.decode(visitPath);
        }catch (Exception e){
            e.printStackTrace();
            res = "error";
        }finally {

            File myfile = new File(visitPath);
            if(myfile.exists()){
                Boolean flag = myfile.delete();
                if (flag){
                    System.out.println("成功删除图片"+myfile.getName());
                }else {
                    System.out.println("删除失败");
                }
            }
        }
        System.out.println(res);
        if(res.equals("error")){
            return "error";
        }else{
            String[] resarr = res.split("/");
            System.out.println(resarr.length);
            res = resarr[3].toString();
        }
        System.out.println(res);

        return res;
    }




}
